package com.sy.controller;

import com.sy.bean.pucharse.Condition.PurQueryCondition;
import com.sy.bean.pucharse.PurQuery;
import com.sy.bean.pucharse.Purchase;
import com.sy.bean.pucharse.AuditStatus;
import com.sy.bean.pucharse.excel.PurchaseExcel;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.exception.ValidateException;
import com.sy.common.utils.BeanOfMap;
import com.sy.service.PurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "采购单管理")
@RequestMapping("/api")
public class PurchaseController extends BaseController {
    @Autowired
    PurchaseService purchaseService;

    @ApiOperation("保存采购单")
    @PostMapping("/purchase")
    public ResultBean save(@RequestBody Purchase purchase) {
        int rs = purchaseService.save(purchase);
        return gotoResult(rs, "保存");
    }

    @ApiOperation("采购单列表")
    @PostMapping("/purchase/list")
    public ResultBean<PurQuery> purQuery(@RequestBody PurQueryCondition condition) {
        PageBean pageBean = purchaseService.query(BeanOfMap.bom(condition));
        return success("采购列表", pageBean);
    }
    @ApiOperation("导出采购单列表")
    @PostMapping("/purchase/exportPru")
    public ResultBean<PurchaseExcel> exportPru(@RequestBody Map map){
        String url = purchaseService.exportPru(map);
        return success("导出采购单列表",url);
    }

    @ApiOperation("采购单详情")
    @GetMapping("/purchase")
    public ResultBean get(int id) {
        Purchase purchase = purchaseService.get(id);
        return success("购物单详情", purchase);
    }
    @PutMapping("/purchase")
    @ApiOperation("修改采购单")
    public ResultBean update(@RequestBody Purchase purchase){
        if (null==purchase.getPurchaseDetailList()||purchase.getPurchaseDetailList().size()==0)
            throw new ValidateException("purchaseDetailList","采购明细不能为空!");
        purchase.getPurchaseDetailList().forEach(o->{
            if (null==o.getGoodsname()||"".equals(o.getGoodsname()))
                throw new ValidateException("Goodsname","商品名称不能为空!");
        });
        int rs = purchaseService.update(purchase);
        return gotoResult(rs,"编辑");
    }
    @ApiOperation("批量删除采购单")
    @DeleteMapping("/purchase")
    public ResultBean delete(@RequestParam("ids") List<Integer> ids){
        int rs = purchaseService.batchDelete(ids);
        return rs>0?success("删除成功"):fail("删除失败");
    }
    @ApiOperation("批量审核采购单")
    @PutMapping("/purchase/status")
    public ResultBean status(@RequestBody AuditStatus status){
        int rs = purchaseService.batchStatus(status);
        return rs>0?success("审核成功"):fail("审核失败");
    }
}